package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.wms.mapper.WmsMaterialStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.WmsMaterialuseMapper;
import com.ruoyi.wms.domain.WmsMaterialuse;
import com.ruoyi.wms.service.IWmsMaterialuseService;
import com.ruoyi.common.core.text.Convert;

/**
 * 物料使用量Service业务层处理
 * 
 * @author chao
 * @date 2024-07-28
 */
@Service
public class WmsMaterialuseServiceImpl implements IWmsMaterialuseService 
{
    @Autowired
    private WmsMaterialuseMapper wmsMaterialuseMapper;

    @Autowired
    private WmsMaterialStockMapper wmsMaterialStockMapper;

    /**
     * 查询物料使用量
     * 
     * @param id 物料使用量主键
     * @return 物料使用量
     */
    @Override
    public WmsMaterialuse selectWmsMaterialuseById(Long id)
    {
        return wmsMaterialuseMapper.selectWmsMaterialuseById(id);
    }

    /**
     * 查询物料使用量列表
     * 
     * @param wmsMaterialuse 物料使用量
     * @return 物料使用量
     */
    @Override
    public List<WmsMaterialuse> selectWmsMaterialuseList(WmsMaterialuse wmsMaterialuse)
    {
        return wmsMaterialuseMapper.selectWmsMaterialuseList(wmsMaterialuse);
    }

    /**
     * 新增物料使用量
     * 
     * @param wmsMaterialuse 物料使用量
     * @return 结果
     */
    @Override
    public int insertWmsMaterialuse(WmsMaterialuse wmsMaterialuse)
    {
        return wmsMaterialuseMapper.insertWmsMaterialuse(wmsMaterialuse);
    }

    /**
     * 修改物料使用量
     * 
     * @param wmsMaterialuse 物料使用量
     * @return 结果
     */
    @Override
    public int updateWmsMaterialuse(WmsMaterialuse wmsMaterialuse)
    {
        return wmsMaterialuseMapper.updateWmsMaterialuse(wmsMaterialuse);
    }

    /**
     * 批量删除物料使用量
     * 
     * @param ids 需要删除的物料使用量主键
     * @return 结果
     */
    @Override
    public int deleteWmsMaterialuseByIds(String ids)
    {
        return wmsMaterialuseMapper.deleteWmsMaterialuseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除物料使用量信息
     * 
     * @param id 物料使用量主键
     * @return 结果
     */
    @Override
    public int deleteWmsMaterialuseById(Long id)
    {
        return wmsMaterialuseMapper.deleteWmsMaterialuseById(id);
    }

    @Override
    public int excuteOutStorge(Long rowId, Long materialId, Double num) {
        int stock = wmsMaterialStockMapper.selectStockByMaterialId(materialId,num);

        if(stock < 0){
            return -1;
        }

        else{
            wmsMaterialuseMapper.updateStatusById(rowId);
            return wmsMaterialStockMapper.excuteOutStorge(materialId,num);
        }

    }
}
